﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;
namespace CricketHub
{
    public class Webbrowser_requst
    {
        HttpWebRequest request;
        public Webbrowser htt;
        public void htm()
        {
            try
            {
                App a = App.Current as App;
                string url = a.link;
                request = (HttpWebRequest)WebRequest.Create(url);//("http://www.espncricinfo.com/india-v-new-zealand-2012/content/story/580394.html?CMP=OTC-RSS");
                IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(Resp), this);
                System.Diagnostics.Debug.WriteLine("requesting to news channel....."+url);
            }
            catch (Exception ee)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Please check network Connection or server side problem");
                });
                
            }
        }
        StringBuilder browserdata = new StringBuilder();
        private void Resp(IAsyncResult asyn)
        {
            try
            {
                Webbrowser_requst ht = (Webbrowser_requst)asyn.AsyncState;
                HttpWebRequest req2 = request;
                HttpWebResponse resp = (HttpWebResponse)req2.EndGetResponse(asyn);
                Stream stream = resp.GetResponseStream();
                StreamReader read = new StreamReader(stream);
                string webdata = read.ReadToEnd();
                int index = webdata.IndexOf("<nobr><font");
                string ss = webdata.Substring(index - 1000);
                int ind = ss.IndexOf("<img src");
                browserdata.Append("<html><body><table style='margin-top:5px;' width='320' align='right' border='0' cellpadding='0' cellspacing='0'><tr> <td width='10' height='1'></td><td class='phototbl'> ");
                string img_source = ss.Substring(ss.IndexOf("<img src"), (ss.IndexOf("<br>") - ss.IndexOf("<img src")));
                string replace_source = img_source.Replace("/db/", "http://www.espncricinfo.com/db/");
                browserdata.Append(replace_source);
                browserdata.Append("</td></table>");

                int lastind = ss.IndexOf("<br>");
                string dat = webdata.Substring(index);
                string data_content = dat.Substring(dat.IndexOf("</p>"));

                int indexo = dat.IndexOf("<!-- body");

                string wanted_data = dat.Substring(dat.IndexOf("</p>"), ((dat.IndexOf("<!-- body") - 10 - (dat.IndexOf("</p>")))));
                browserdata.Append(wanted_data);
                browserdata.Append("</body></html>");
                string esp = browserdata.ToString();
                string actualcontent = ss.Substring(ind, (lastind - ind));

                htt.Browser_data(esp);
                System.Diagnostics.Debug.WriteLine("response from the requesting data........" + esp);
            }
            catch (Exception ees)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show("Please check network Connection or server side problem");
                    });
            }


        }

    }
}
